<template>
  <el-card class="constraint-panel">
    <div slot="header">
      <span>排课约束条件配置</span>
    </div>
    <el-form ref="constraintForm" :model="constraintWeights" :rules="rules" label-width="120px">
      <!-- 基本约束 -->
      <el-form-item label="课程冲突" prop="courseConflict">
        <el-slider v-model="constraintWeights.courseConflict" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>

      <!-- 教师相关约束 -->
      <el-form-item label="教师偏好" prop="teacherPreference">
        <el-slider v-model="constraintWeights.teacherPreference" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>
      <el-form-item label="教师时间冲突" prop="teacherTimeConflict">
        <el-slider v-model="constraintWeights.teacherTimeConflict" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>
      <el-form-item label="教师工作量" prop="teacherWorkload">
        <el-slider v-model="constraintWeights.teacherWorkload" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>

      <!-- 教室相关约束 -->
      <el-form-item label="教室容量" prop="classroomCapacity">
        <el-slider v-model="constraintWeights.classroomCapacity" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>
      <el-form-item label="教室时间冲突" prop="roomTimeConflict">
        <el-slider v-model="constraintWeights.roomTimeConflict" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>
      <el-form-item label="教室设备要求" prop="roomEquipReq">
        <el-slider v-model="constraintWeights.roomEquipReq" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>

      <!-- 班级相关约束 -->
      <el-form-item label="班级时间冲突" prop="classTimeConflict">
        <el-slider v-model="constraintWeights.classTimeConflict" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>
      <el-form-item label="班级课程分布" prop="classDistribution">
        <el-slider v-model="constraintWeights.classDistribution" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>

      <!-- 课程相关约束 -->
      <el-form-item label="连堂课" prop="continuousClass">
        <el-slider v-model="constraintWeights.continuousClass" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>
      <el-form-item label="课程优先级" prop="coursePriority">
        <el-slider v-model="constraintWeights.coursePriority" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>
      <el-form-item label="特殊时间要求" prop="specialTimeReq">
        <el-slider v-model="constraintWeights.specialTimeReq" :min="0" :max="100" :step="5" show-input></el-slider>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm">保存配置</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: 'ConstraintConfigPanel',
  props: {
    value: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      constraintWeights: {
        teacherPreference: 50,    // 教师偏好
        classroomCapacity: 80,    // 教室容量
        courseConflict: 100,      // 课程冲突
        teacherTimeConflict: 90,  // 教师时间冲突
        classTimeConflict: 90,    // 班级时间冲突
        roomTimeConflict: 90,     // 教室时间冲突
        continuousClass: 70,      // 连堂课
        teacherWorkload: 60,      // 教师工作量
        coursePriority: 50,       // 课程优先级
        specialTimeReq: 80,       // 特殊时间要求
        roomEquipReq: 60,         // 教室设备要求
        classDistribution: 70     // 班级课程分布
      },
      rules: {
        teacherPreference: [{ required: true, message: '请设置教师偏好权重', trigger: 'change' }],
        classroomCapacity: [{ required: true, message: '请设置教室容量权重', trigger: 'change' }],
        courseConflict: [{ required: true, message: '请设置课程冲突权重', trigger: 'change' }],
        teacherTimeConflict: [{ required: true, message: '请设置教师时间冲突权重', trigger: 'change' }],
        classTimeConflict: [{ required: true, message: '请设置班级时间冲突权重', trigger: 'change' }],
        roomTimeConflict: [{ required: true, message: '请设置教室时间冲突权重', trigger: 'change' }],
        continuousClass: [{ required: true, message: '请设置连堂课权重', trigger: 'change' }],
        teacherWorkload: [{ required: true, message: '请设置教师工作量权重', trigger: 'change' }],
        coursePriority: [{ required: true, message: '请设置课程优先级权重', trigger: 'change' }],
        specialTimeReq: [{ required: true, message: '请设置特殊时间要求权重', trigger: 'change' }],
        roomEquipReq: [{ required: true, message: '请设置教室设备要求权重', trigger: 'change' }],
        classDistribution: [{ required: true, message: '请设置班级课程分布权重', trigger: 'change' }]
      }
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val) {
          this.constraintWeights = { ...this.constraintWeights, ...val }
        }
      },
      immediate: true
    },
    constraintWeights: {
      handler(val) {
        this.$emit('input', val)
        this.$emit('change', val)
      },
      deep: true
    }
  },
  methods: {
    submitForm() {
      this.$refs.constraintForm.validate(valid => {
        if (valid) {
          this.$emit('submit', this.constraintWeights)
          this.$message.success('约束条件配置已保存')
        }
      })
    },
    resetForm() {
      this.$refs.constraintForm.resetFields()
    }
  }
}
</script>

<style scoped>
.constraint-panel {
  margin-bottom: 20px;
}
.el-slider {
  width: 100%;
}
</style>